/*
 * Created on 2006-2-25
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.toyi.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.toyi.util.MenuUtil;

/**
 * @author yangbingxp
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class LoginServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//        System.out.println("------------do get");
//        if (req.getSession(false) != null) {
//            System.out.println(req.getSession(false).getId());
//        }
        doPost(req, res);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String action = req.getParameter("action");
//        System.out.println("action===" + action);
//        System.out.println(SecurityConstants.getSecurityConstant(SecurityConstants.MAIN_MENU_PAGE));
//        System.out.println("----------------");
        try {
            if (action.equalsIgnoreCase("login")) {
                String userId = req.getParameter("userId");
                String password = req.getParameter("password");
                UserInfo user = LoginManager.authenticateUser(userId, password);
                if (user == null) {
                    req.getRequestDispatcher(SecurityConstants.getSecurityConstant(SecurityConstants.LOGIN_FAIL_PAGE))
                            .forward(req, res);
                } else {
					
                    HttpSession session = req.getSession(false);
                    if(session!=null) session.invalidate();
					session=req.getSession(true);
                    session.setAttribute(SecurityConstants.USERINFO_ATTR, user);
//                    System.out.println("set user's session id======="+session.getId());
                    
                    String menuType = MenuUtil.getProperty("menu.type");
//                    System.out.println("menuType----------------------->" + menuType);
                    if (menuType.equalsIgnoreCase("tree")) {
                        session.setAttribute(SecurityConstants.MENU_ATTR, user.getMenuString() == null ? "" : user
                                .getMenuString());
                    }
                    req.getRequestDispatcher(SecurityConstants.getSecurityConstant(SecurityConstants.WELCOME_PAGE))
                            .forward(req, res);
                }
            } else {
                //logout
                if (req.getSession(false) != null) {
                    req.getSession(false).invalidate();
                }
                req.getRequestDispatcher(SecurityConstants.getSecurityConstant(SecurityConstants.LOGOUT_PAGE)).forward(
                        req, res);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}